package com.web.graduationpro.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.web.graduationpro.common.CommonDataClass;
import com.web.graduationpro.dao.service.ArticleService;
import com.web.graduationpro.dao.service.GoodsService;
import com.web.graduationpro.dao.service.InstrumentSchoolService;
import com.web.graduationpro.dao.service.TeacherService;
import com.web.graduationpro.model.Article;
import com.web.graduationpro.util.PageUtil;

@Controller
@RequestMapping("/article/*")
public class ArticleController {

	@Resource(name="articleServiceImpl")
	private ArticleService articleServiceImpl;
	
	@Resource(name="instrumentSchoolServiceImpl")
	private InstrumentSchoolService instrumentSchoolServiceImpl;
	
	@Resource(name="teacherServiceImpl")
	private TeacherService teacherServiceImpl;
	
	@Resource(name="goodsServiceImpl")
	private GoodsService goodsServiceImpl;
	
	@RequestMapping(value = "/index", method = RequestMethod.GET)  
    public String index(HttpServletRequest request,HttpServletResponse reponse) {  
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		reponse.setContentType("text/html;charset=UTF-8");
		
		//查询数据的HQL语句
		String queryHql = "  FROM Article a WHERE a.id!=0";
		//查询总数的HQL语句
	    String queryCountHql = "SELECT count(*) From Article a WHERE a.id!=0 ";
		
		//参数值集合
		List<Object> searchValues = new ArrayList<Object>();
		//参数值数组
		Object[] cses = null; 
		
		//当前文章的类型
		int articleType = 1;
		String articleTypeValue = "最新开课";
		String articleTypeStr = request.getParameter("type");
		if(articleTypeStr!=null && !"".equals(articleTypeStr.trim()) && ("1".equals(articleTypeStr) || "2".equals(articleTypeStr))){
			articleType = Integer.parseInt(articleTypeStr);
		}
		if(articleType==2){
			articleTypeValue = "行业新闻";
		}
		request.setAttribute("articleTypeValue", articleTypeValue);
		
		if(articleType!=0){
			searchValues.add(articleType);
			queryHql += " AND a.articleType.id=?";
	    	queryCountHql += " AND a.articleType.id=?";
		}

		/*************************分页开始****************************/
		String actionUrl = "/GraduationPro/article/index.do?type="+articleType+"&";
		int totalNum = 0;
		int showNum = 5;
		String currPageStr = request.getParameter("currPage");
		String pageSizeStr = request.getParameter("pageSize");
		int currPage = 1;
		if(currPageStr!=null && !"".equals(currPageStr.trim())){
			currPage = Integer.parseInt(currPageStr);
		}
		int pageSize = 20;
		if(pageSizeStr!=null && !"".equals(pageSizeStr.trim())){
			pageSize = Integer.parseInt(pageSizeStr);
		}
		
        if(searchValues!=null && searchValues.size()!=0){
        	cses = new Object[searchValues.size()];
        	for(int i=0;i<searchValues.size();i++){
        		cses[i] = searchValues.get(i);
        	}
        	totalNum = articleServiceImpl.findTotalCount(queryCountHql, cses);
        }else{
        	totalNum = articleServiceImpl.findTotalCount(queryCountHql);	
        }
		List<String> page = null;
		if(totalNum>0){
			page = PageUtil.getInstance().getPage(actionUrl, totalNum, showNum, currPage, pageSize);
		}
		request.setAttribute("page", page);
		/*************************分页结束****************************/
		
		/*************************查询数据开始****************************/
		List<Article> queryDatas = null;
		if(searchValues!=null && searchValues.size()!=0){
			queryDatas = articleServiceImpl.findByPage(queryHql,cses,((currPage-1)*pageSize), pageSize);
		}else{
			queryDatas = articleServiceImpl.findByPage(queryHql,((currPage-1)*pageSize), pageSize);	
        }
		if(queryDatas!=null && queryDatas.size()!=0){
			request.setAttribute("queryDatas", queryDatas);
		}

		new CommonDataClass(instrumentSchoolServiceImpl,teacherServiceImpl,goodsServiceImpl,articleServiceImpl,request);
		/*************************查询数据结束****************************/
		return "/article";
    }  
	
	@RequestMapping(value = "/info", method = RequestMethod.GET)  
    public String info(HttpServletRequest request,HttpServletResponse reponse) {  
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		reponse.setContentType("text/html;charset=UTF-8");
		
		String articleType = "最新开课";
		String id = request.getParameter("id");
		if(id!=null && !"".equals(id.trim())){
			Article data = (Article)articleServiceImpl.findById(Article.class, Integer.parseInt(id));
			
			request.setAttribute("type", data.getArticleType().getId());
			if(data.getArticleType().getId()==2){
				articleType = "行业新闻";
			}
			request.setAttribute("articleType", articleType);
			
			request.setAttribute("data", data);
		}
		new CommonDataClass(instrumentSchoolServiceImpl,teacherServiceImpl,goodsServiceImpl,articleServiceImpl,request);
        
		return "/articleinfo";
    }  
}
